#############################
##BashMUploader		    #
##Script Bash by Benpro     #
##Mail : benpro82@gmail.com #
##Version : 0.2		    #
##Last Revision : 17-04-09  #
##Licence : GPLv3	    #
#############################
#/!\Saisissez votre login et pass MU/!\
login=
pass=

#Ne pas toucher au reste :)
start=1
echo "Welcome to BashMUploader V0.2"
if [ -z "$1" ]; then
  echo "Il manque le fichier à uploader."
  echo "$0 nom_du_fichier"
  start=0
fi

filename=$1

testnumber() {
  number=$(echo $RANDOM | cut --bytes=1,2,3)
  while test $number -gt 799 -o $number -lt 200
  do number=$(echo $RANDOM | cut --bytes=1,2,3); done;
  echo "On test le serveur n°"$number" pour voir si il accepte les posts"
  verifyserver=$(ping www$number.megaupload.com -c 1 | egrep --only-matching "hosted.by.cirn.net")
  if [ -z "$verifyserver" ]; then
    mupload
  else
    testnumber
  fi
}

mupload() {
  connex=$(curl -s "www.megaupload.com/?c=login" -c muploadcookies --data-urlencode login=1 --data-urlencode username=$login --data-urlencode password=$pass)
  user=$(cat muploadcookies | grep user | awk '{print $7}')
  id=$(cat muploadcookies | grep user | awk '{print $5}')
  server="www$number.megaupload.com"
  adr="upload_done.php?UPLOAD_IDENTIFIER=$id&user=$user"
  
  echo "Serveur numero :"$number - $server
  lien=$(curl -# --user-agent "Shockwave Flash" "$server/$adr" --form "Filename=$filename" --form-string "message=Uploaded by BashMUploader -- by benpro" --form "Filedata=@$filename;type=application/octet-stream" --form-string "user=$user")
  lien=$(echo $lien | egrep --only-matching "http://www.megaupload.com/[^']*") #awk '{print $17}' | sed s"/'//" | sed s"/'//" | sed s"/;//")
  start=0  
  echo "Voici le lien: "$lien
  if [ -z "$lien" ]; then
    echo "Erreur on reupload ;)"
    testnumber
  fi
}

if [ $start = 1 ]; then
  testnumber
fi

if [ $start = 0 ]; then
  exit
fi
